package com.houtai.service.article;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.TsArticleComments;
import com.houtai.bean.User;
import com.houtai.bean.dto.article.ArticleDetailDto;
import com.houtai.bean.dto.article.ArticleListDto;
import com.houtai.bean.dto.article.comments.TsArticleCommentsInsertDto;
import com.houtai.bean.dto.article.comments.TsArticleCommentsListDto;
import com.houtai.bean.vo.article.ArticleDetail;
import com.houtai.bean.vo.article.ArticleDetailVo;
import com.houtai.bean.vo.article.ArticleList;
import com.houtai.bean.vo.article.ArticleListVo;
import com.houtai.bean.vo.article.cate.TsArticleCateList;
import com.houtai.bean.vo.article.cate.TsArticleCateListVo;
import com.houtai.bean.vo.article.comments.TsArticleCommentsList;
import com.houtai.bean.vo.article.comments.TsArticleCommentsListVo;
import com.houtai.bean.vo.article.comments.TsArticleCommentsReplyList;
import com.houtai.common.util.WordsUtil;
import com.houtai.dao.TsArticleCateDao;
import com.houtai.dao.TsArticleCommentsDao;
import com.houtai.dao.TsJzArticleInfoDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {
    private static Logger logger = Logger.getLogger(ArticleService.class);
    @Autowired
    private TsArticleCateDao tsArticleCateDao;
    @Autowired
    private TsJzArticleInfoDao tsJzArticleInfoDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsArticleCommentsDao tsArticleCommentsDao;
    /**
     * @Purpose:    文章列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public ArticleListVo articleList(ArticleListDto dto) {
        String logInfo = this.getClass().getName() + ":articleList:";
        ArticleListVo retInfo = new ArticleListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "arti_scort DESC, id DESC");
            List<ArticleList> lists = tsJzArticleInfoDao.findArticleList(dto);
            PageInfo<ArticleList> list = new PageInfo<>(lists);
            retInfo.setR_list(tsJzArticleInfoDao.findRecommendedArticleList());
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无文章");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    文章类别列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TsArticleCateListVo
     */
    public TsArticleCateListVo articleCateList() {
        String logInfo = this.getClass().getName() + ":articleCateList:";
        TsArticleCateListVo retInfo = new TsArticleCateListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            List<TsArticleCateList> lists = tsArticleCateDao.findTsArticleCateList();
            if (lists != null && lists.size() > 0) {
                retInfo.setList(lists);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无文章类别");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    文章详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ArticleDetailVo articleDetail(ArticleDetailDto dto) {
        String logInfo = this.getClass().getName() + ":articleDetail:";
        ArticleDetailVo retInfo = new ArticleDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            retInfo.setR_list(tsJzArticleInfoDao.findRecommendedArticleList());
            ArticleDetail articleDetail = tsJzArticleInfoDao.findArticleDetail(dto.getId());
            if (articleDetail != null) {
                retInfo.setArticle(articleDetail);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("文章不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    文章评论
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo articleComments(TsArticleCommentsInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":articleComments:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            ArticleDetail articleDetail = tsJzArticleInfoDao.findArticleDetail(dto.getArticle_id());
            if (articleDetail != null) {
                if ( articleDetail.getIs_comments().equals(0)) {
                    if (WordsUtil.isExit(dto.getComments_content())) {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TsArticleComments tsArticleComments = new TsArticleComments();
                        tsArticleComments.setArticle_id(dto.getArticle_id());
                        tsArticleComments.setComments_content(dto.getComments_content());
                        tsArticleComments.setIs_reply(dto.getComments_id());
                        tsArticleComments.setUser_id(user.getUser_id());
                        tsArticleCommentsDao.insertTsArticleComments(tsArticleComments);
                        if (dto.getComments_id() == 0) {
                            int size = tsArticleCommentsDao.findSize(tsArticleComments.getUser_id(), tsArticleComments.getArticle_id());
                            articleDetail.setComments_size(articleDetail.getComments_size() + 1);
                            if (size == 0) {
                                articleDetail.setComments_people_size(articleDetail.getComments_people_size() + 1);
                            }
                            tsJzArticleInfoDao.updateArticleCommentsSize(articleDetail);
                        }

                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("评论包含敏感词");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("文章不可评论");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("文章不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    文章评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public TsArticleCommentsListVo commentsList(TsArticleCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":commentsList:";
        TsArticleCommentsListVo retInfo = new TsArticleCommentsListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TsArticleCommentsList> lists = tsArticleCommentsDao.findTsArticleCommentsList(dto);
            PageInfo<TsArticleCommentsList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                for (int i = 0; i < list.getList().size(); i ++){
                    List<TsArticleCommentsReplyList> reply = tsArticleCommentsDao.findTsArticleCommentsReplyList(list.getList().get(i).getComments_id());
                    list.getList().get(i).setReplys(reply);
                }

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无评论");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
